# -*- encoding=utf8 -*-
__author__ = "Administrator"
"""批量刷新宝贝，批量上架已经下架的宝贝"""
import sys,os,argparse
sys.path.append(os.getcwd())
from auto_refresh_fun import *
from airtest.core.api import *
parser = argparse.ArgumentParser(description='manual to this script')
parser.add_argument('--devices', type=str, default = None)
args = parser.parse_args()
if args.devices:
    devices_uri="android://127.0.0.1:5037/%s?cap_method=javacap&touch_method=adb"%(args.devices)
else:
    devices_uri="android:///"
connect_device(devices_uri)
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
x,y = poco.get_screen_size() #获取屏幕分辨率

#--------------初始化咸鱼app-----------------------    
package = "com.taobao.idlefish"
init_idle_fish(package)

#--------------提示升级，点击不升级-----------------------
sleep(15)
if poco(text='暂不升级').exists():
    poco(text='暂不升级').click()
#------------------------------------------------------------

for count_time in range(40):
    try:
        # #----------批量擦亮宝贝--------------------------
        wait_for_appearance_and_click(poco(nameMatches='^我的.*状态$',selected=False))
        wait_for_appearance_and_click(poco(textMatches='^我发布的.*$',touchable=True))
        pos = (0.8*x,0.33*y) #一键擦亮按钮
        touch(pos)
        sleep(1)
        touch(pos)
#         for _ in range(15):
#             if poco(desc='擦亮',touchable=True).exists():
#                 refresh_list = poco(desc='擦亮',touchable=True)
#                 refresh_count = len(refresh_list)
#                 for i in range(refresh_count):
#                     refresh_list[i].click()
#             else:
#                 swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
#                 sleep(1)
#                 swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
        
        #----------批量重新上架宝贝--------------------------
        wait_for_appearance_and_click(poco(name='下架宝贝',touchable=False))
        while True:
            sleep(2)
            if poco(name="com.taobao.idlefish:id/more_close_div").exists():
                keyevent("BACK")
            afresh_list = poco(text='重新发布',touchable=True)
            afresh_count = len(afresh_list)
            print ("当前页面有%s个商品待重新发布"%afresh_count)
            for i in range(afresh_count):
                afresh_list[i].click()
                sleep(3)
                wait_for_appearance_and_click(poco(text='确认发布',touchable=True))
                sleep(5)
                if poco(name='发布成功',desc='发布成功').exists(): #点完确认发布后,跳转到发布成功的页面
                    keyevent("BACK")
                    sleep(3)
                    if poco(text='已签到',touchable=True).exists():
                        keyevent("BACK")
                        sleep(1)
                    elif poco(text='签到',touchable=True).exists(): 
                        poco(text='签到',touchable=True).click()
                        sleep(2)
                        keyevent("BACK")
                    elif poco(text='管理',touchable=True).exists():
                        keyevent("BACK")
                    elif poco(text='鱼塘货架',touchable=False).exists():
                        keyevent("BACK")
                elif poco(text='签到',touchable=True).exists(): #点完确认发布后,跳转到鱼塘签到页面
                    poco(text='签到',touchable=True).click()
                    sleep(2)
                    keyevent("BACK")
                    sleep(1)
                    keyevent("BACK")
                elif poco(text='已签到',touchable=True).exists():
                    keyevent("BACK")
                    sleep(1)
                    keyevent("BACK")
                sleep(1)
            swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
            if not poco(text='售出下架').exists():
                keyevent("BACK")
                break
    except Exception as e:
        print ("出现异常:",e)
        init_idle_fish(package)
        #--------------提示升级，点击不升级-----------------------
        sleep(15)
        if poco(text='暂不升级').exists():
            poco(text='暂不升级').click()

#     # #----------批量删除下架的宝贝宝贝--------------------------
#     if delete_baby:
#         wait_for_appearance_and_click(poco(name='下架宝贝',touchable=False))
#         while True:
#             sleep(2)
#             try:
#                 more_list = poco(desc='更多',touchable=True)
#                 more_count = len(more_list)
#                 for i in range(more_count):
#                     more_list[i].click()
#                     sleep(1)
#                     wait_for_appearance_and_click(poco(text='删除',touchable=True))
#                     wait_for_appearance_and_click(poco(text='确定',touchable=True))
#                 swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
#             except:
#                 a +=1 
#             if a == 10:
#                 break

    
    